package com.brillilab.service.core.system.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.kit.KitMapper;
import com.brillilab.dao.mapper.system.CompanyMapper;
import com.brillilab.domain.po.system.Company;
import com.brillilab.domain.vo.system.CompanyGroupVo;
import com.brillilab.service.core.system.ICompanyService;

/**
 * 地区服务
 *
 * @author wmh
 */
@Service
public class CompanyServiceImpl implements ICompanyService {

	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private KitMapper kitMapper;

	@Override
	public Company insert(Company entity) {
		Integer insert = companyMapper.insert(entity);
		Assert.isTrue(insert == 1, "添加失败");
		return entity;
	}

	@Override
	public boolean deleteById(Long id) {
		Integer b = companyMapper.deleteById(id);
		return b == 1 ? true : false;
	}

	@Override
	public boolean updateById(Company company) {
		Integer b = companyMapper.updateById(company);
		return b == 1 ? true : false;
	}

	@Override
	public Company selectById(Long id) {
		return companyMapper.selectById(id);
	}

	@Override
	public List<CompanyGroupVo> getCompanyGroup() {
		List<CompanyGroupVo> groupVoList = new ArrayList<CompanyGroupVo>();
		List<Company> list = companyMapper.selectList(null);
		// 对公司字典进行分类，以每个tab下面必须大于20各公司的算法来归类
		List<String> grouplist = new ArrayList<String>();
		String tab = "";
		int count = 0;
		for (char index = 'A'; index <= 'Z'; index++) {
			for (Company item : list) {
				String letter = String.valueOf(index);
				if (item.getName().toUpperCase().startsWith(letter))
					count++;
			}

			if (count <= 20) {
				if (StringUtils.isBlank(tab)) {
					tab = String.valueOf(index);
				} else {
					tab += "," + String.valueOf(index);
				}
				if (index == 'Z'){
					grouplist.add(tab);
				}
			} else {
				if (StringUtils.isBlank(tab)){
					tab = String.valueOf(index);
				} else{
					tab += "," + String.valueOf(index);
				}
				grouplist.add(tab);
				count = 0;
				tab = "";
			}
		}

		//数据归组
		for (String group : grouplist) {
			List<Company> companyList =  new ArrayList<>();
			String[] sArr = group.split(",");
			for (String starts : sArr) {
				for (Iterator<Company> iterator = list.iterator(); iterator.hasNext();) {
					Company company = iterator.next();
					if (company.getName().toUpperCase().startsWith(starts)){
						companyList.add(company);
						iterator.remove();
					}
				}
			}
			CompanyGroupVo vo = new CompanyGroupVo();
			vo.setGroupName(group);
			vo.setCompanyList(companyList);
			groupVoList.add(vo);
		}
		CompanyGroupVo vo = new CompanyGroupVo();
		vo.setGroupName("其余");
		vo.setCompanyList(list);
		groupVoList.add(vo);
		return groupVoList;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Company> getCompanyList(int isLab, Integer classify, Long labId) {
		if(isLab==1) {
			return kitMapper.getCompanyList(classify, labId);
		}else {
			LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
			wrapper.orderByAsc(Company::getName);
			return companyMapper.selectList(wrapper);
		}
	}

	@Override
	public List<Company> searchCompanyByName(String namePrefix) {
		LambdaQueryWrapper<Company> query = new LambdaQueryWrapper<>();
		query.likeRight(Company::getName,namePrefix).last("limit 10");
		return companyMapper.selectList(query);
	}
}
